package com.vst.orm.dao.hibernate.original;

import java.io.Serializable;
import java.util.List;
import org.hibernate.NonUniqueResultException;

import com.vst.orm.dao.DAOUtil;
import com.vst.orm.dao.hibernate.HibernateBaseDAO;
import com.vst.orm.search.ExampleOptions;
import com.vst.orm.search.Filter;
import com.vst.orm.search.ISearch;
import com.vst.orm.search.Search;
import com.vst.orm.search.SearchResult;

public class GenericDAOImpl<T, ID extends Serializable> extends HibernateBaseDAO implements GenericDAO<T, ID> {
	protected Class<T> persistentClass = (Class) DAOUtil.getTypeArguments(GenericDAOImpl.class, getClass()).get(0);

	public void create(T object) {
		if (!this.persistentClass.isInstance(object)) {
			throw new IllegalArgumentException("Object class does not match dao type.");
		}
		_save(object);
	}

	public boolean createOrUpdate(T object) {
		Serializable id = getMetaDataUtil().getId(object);
		if ((id == null) || (new Long(0L).equals(id))) {
			create(object);
			return true;
		}
		update(object);
		return false;
	}

	public boolean deleteById(ID id) {
		return _deleteById(this.persistentClass, id);
	}

	public boolean deleteEntity(T object) {
		if (!this.persistentClass.isInstance(object)) {
			throw new IllegalArgumentException("Object class does not match dao type.");
		}
		return _deleteEntity(object);
	}

	public T fetch(ID id) {
		return (T) _get(this.persistentClass, id);
	}

	public List<T> fetchAll() {
		return _all(this.persistentClass);
	}

	public void update(T object) {
		if (!this.persistentClass.isInstance(object)) {
			throw new IllegalArgumentException("Object class does not match dao type.");
		}
		_update(new Object[] { object });
	}

	public List<T> search(ISearch search) {
		if (search == null) {
			return fetchAll();
		}
		return _search(this.persistentClass, search);
	}

	public int count(ISearch search) {
		if (search == null) {
			search = new Search();
		}
		return _count(this.persistentClass, search);
	}

	public SearchResult<T> searchAndCount(ISearch search) {
		if (search == null) {
			SearchResult<T> result = new SearchResult();
			result.setResult(fetchAll());
			result.setTotalCount(result.getResult().size());
			return result;
		}
		return _searchAndCount(this.persistentClass, search);
	}

	public boolean isConnected(Object object) {
		return _sessionContains(object);
	}

	public void flush() {
		_flush();
	}

	public void refresh(Object object) {
		_refresh(new Object[] { object });
	}

	public List searchGeneric(ISearch search) {
		if (search == null) {
			return fetchAll();
		}
		return _search(this.persistentClass, search);
	}

	public Object searchUnique(ISearch search) throws NonUniqueResultException {
		return _searchUnique(this.persistentClass, search);
	}

	public Filter getFilterFromExample(T example) {
		return _getFilterFromExample(example);
	}

	public Filter getFilterFromExample(T example, ExampleOptions options) {
		return _getFilterFromExample(example, options);
	}
}
